package com.shopAttr.service.impl;

import com.shop.bean.Shop;
import com.shop.feign.ShopFeign;
import com.shopAttr.bean.ShopAttr;
import com.shopAttr.dao.ShopAttrMapper;
import com.shopAttr.service.IShopAttrService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shopBean.common.bean.RestResult;
import shopBean.common.responseCode.Status;
import shopBean.common.utils.DateCommonUtils;
import shopBean.common.utils.RestResultGenerator;
import shopBean.common.utils.UUIDGenerator;

import java.util.List;


@Service("shopAttrService")
public class ShopAttrServiceImpl implements IShopAttrService {

    @Autowired
    ShopFeign shopFeign;
    @Autowired
    ShopAttrMapper shopAttrMapper;

    @Override
    public RestResult<ShopAttr> insert(ShopAttr shopAttr) {
        if(StringUtils.isNotBlank(shopAttr.getpId())){
            ShopAttr parentShopAttr = this.shopAttrMapper.get(shopAttr.getpId());
            if(parentShopAttr == null){
                return RestResultGenerator.genErrorResult("父属性不存在" ,null,Status.parent_is_no_exist);
            }
        }
        if(this.shopAttrMapper.getByNameAndShopId(shopAttr.getName(),shopAttr.getShopId()) != 0){
            return RestResultGenerator.genErrorResult("商品属性名称已存在",null,Status.shop_attr_is_exist);
        }
        RestResult<Shop> shop = this.shopFeign.getShopById(shopAttr.getShopId());
        if( shop == null ){
            return RestResultGenerator.genErrorResult("系统繁忙,商品服务不可用 ",null, Status.sys_error);
        }
        if(shop.getData()  == null){
            return RestResultGenerator.genErrorResult("商品不存在",null,Status.shop_is_no_exist);
        }
        shopAttr.setId(UUIDGenerator.generate38());
        shopAttr.setCreateDate(DateCommonUtils.getCurrent());
        shopAttrMapper.insert(shopAttr);
        return RestResultGenerator.genSuccessResult(shopAttr);
    }

    @Override
    public int deleteById(String id) {
        ShopAttr shopAttr = this.shopAttrMapper.get(id);
        if(this.shopAttrMapper.get(id) == null){
            return  0 ;
        }
        this.deleteByIdAll(id);
        return this.shopAttrMapper.delete(id);
    }

    @Override
    public RestResult update(ShopAttr shopAttr) {
        if(this.shopAttrMapper.getByNameAndNoInId(shopAttr.getName(),shopAttr.getId()) ==0){
            return RestResultGenerator.genErrorResult("更新失败，商品属性已存在",null,Status.shop_attr_name_is_exits);
        }

        return RestResultGenerator.genSuccessResult(this.shopAttrMapper.update(shopAttr));
    }

    @Override
    public List<ShopAttr> getFristByShopId(String shopId) {
        return this.shopAttrMapper.getFristByShopId(shopId);
    }

    @Override
    public List<ShopAttr> getByPid(String pid) {
        return this.shopAttrMapper.getByPid(pid);
    }

    @Override
    public List<ShopAttr> getByShopId(String shopId) {
        return this.shopAttrMapper.getByShopId(shopId);
    }

    public void deleteByIdAll(String id){
        List<ShopAttr> shopAttrList = this.shopAttrMapper.getByPid(id);
        if(shopAttrList == null){
            return;
        }
        for (ShopAttr result : shopAttrList){

            this.shopAttrMapper.delete(result.getId());
            this.deleteByIdAll(result.getId());
        }

    }

}
